package com.csis.framework.util.jqgrid;

import com.csis.framework.domain.FormBean;
import com.csis.framework.domain.Page;

public class JqGridXMLResponseParser<E, T extends FormBean<E>> extends
		AbstractFormBeanResponseParser<E, T> {

	@Override
	public JqGridResponse parse(T t) {
		Page page = t.getPage();
		page.prepare();
		StringBuffer buffer = new StringBuffer();
		buffer.append("<?xml version='1.0' encoding='UTF-8'?>");
		buffer.append("<rows>");
		buffer.append("<page>").append(page.getCurPage()).append("</page>");
		buffer.append("<total>").append(page.getMaxPage()).append("</total>");
		buffer.append("<records>").append(page.getMaxCount())
				.append("</records>");
		int count = 0;
		for (E e : t.getBeans()) {
			Cell<E> cell = Cell.create(e);
			cell.setSequence((page.getCurPage() - 1) * page.getCount()
					+ ++count);
			buffer.append("<row id='")
					.append(getFilter().getJqGridCellId(cell)).append("'>");
			Object[] columns = getFilter().getJqGridCell(cell);
			for (Object c : columns)
				buffer.append("<cell><![CDATA[").append(c).append("]]></cell>");
			buffer.append("</row>");
		}
		buffer.append("</rows>");
		return JqGridResponse.build(buffer.toString());
	}
}
